package controllers

import (
	"ShopSupplierApi/models"
	"ShopSupplierApi/services"
	"ShopSupplierApi/utils"
	"time"
)

//OrdersController 订单
type OrdersController struct {
	LcfController
}

// @Title   订单列表
// @Param   sid query   uint64  true    "供应商ID"
// @Param   type  query    int    false  "订单类型"
// @Param   starttime   query   int64   false   "开始时间戳"
// @Param   starttime   endtime int64   false   "结束时间戳"
// @Param   p body  models.Page true    "分页数据信息"
// @Success 200 {object} models.Order
// @router /orderList [post]
func (c *OrdersController) OrderList() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	if err = c.ParseBodyToPage(); err != nil {
		return
	}
	t, _ := c.GetInt("type", 0)
	starttime, _ := c.GetInt64("starttime", 0)
	endtime, _ := c.GetInt64("endtime", time.Now().Unix())
	c.JSONPageData(services.OrderServ.OrderList(uint64(sid), models.OrderType(t), starttime, endtime, c.Data["page"].(*models.Page)))
}

// @Title   订单商品
// @Param   sid query   uint64  true    "供应商ID"
// @Param   oid query   uint64  true    "订单号"
// @Success 200 {object} models.OrderProduct
// @router /orderItem [get]
func (c *OrdersController) OrderItem() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	oid, err := c.GetInt64("oid")
	if err != nil {
		c.JSONFailedError(utils.LoseParam("oid[订单号]"))
		return
	}
	c.JSONSuccess(services.OrderServ.OrderItem(uint64(sid), uint64(oid)))
}

// @Title   订单详情
// @Param   sid query   uint64  true    "供应商ID"
// @Param   oid query   uint64  true    "订单号"
// @Success 200 {object} models.OrderInfo
// @router /orderInfo [get]
func (c *OrdersController) OrderInfo() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	oid, err := c.GetInt64("oid")
	if err != nil {
		c.JSONFailedError(utils.LoseParam("oid[订单号]"))
		return
	}
	order, err := services.OrderServ.OrderInfo(uint64(sid), uint64(oid))
	if err != nil {
		c.JSONFailedError(utils.NotFoundOrder)
		return
	}
	c.JSONSuccess(order)
}

// @Title   商品订单列表
// @Param   sid query   uint64  true "供应商ID"
// @Param   pid query   uint64  true "货品ID"
// @Param   p body  models.Page true    "分页数据信息"
// @Success 200 {object} models.Order
// @router /productOrders [post]
func (c *OrdersController) ProductOrders() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	if err = c.ParseBodyToPage(); err != nil {
		return
	}
	pid, err := c.GetInt64("pid")
	if err != nil {
		c.JSONFailedError(utils.LoseParam("pid[货品ID]"))
	}
	c.JSONPageData(services.OrderServ.ProductOrders(uint64(sid), uint64(pid), c.Data["page"].(*models.Page)))
}

// @Title   订单统计
// @Param   sid query   uint64  true "供应商ID"
// @Param	timeType	query	int	false "时间类型[0=不限,1=年,2=月,3=日]"
// @Param	dataType	query	int	false	"订单统计类型[1=订单,2=金额]"
// @Param	t	query	string	false "日期[yyyy-MM-dd HH:mm:ss]默认:当前时间"
// @Success 200
// @router /orderCount [get]
func (c *OrdersController) OrderCount() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	timeType, _ := c.GetInt("timeType", 0)
	dataType, _ := c.GetInt("dataType", 1)
	t, _ := utils.StringParseTime(c.GetString("t"))
	c.JSONSuccess(services.OrderServ.OrderCount(uint64(sid), models.OrderTimeType(timeType), models.OrderDataType(dataType), t))
}

// @Title   下单时间段统计
// @Param   sid query   uint64  true "供应商ID"
// @Param	timeType	query	int	false "时间类型[0=不限,1=年,2=月,3=日]"
// @Param	t	query	string	false "日期[yyyy-MM-dd HH:mm:ss]默认:当前时间"
// @Success 200
// @router /orderTimeCount [get]
func (c *OrdersController) OrderTimeCount() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	timeType, _ := c.GetInt("timeType", 0)
	t, _ := utils.StringParseTime(c.GetString("t"))
	c.JSONSuccess(services.OrderServ.OrderTimeCount(uint64(sid), models.OrderTimeType(timeType), t))
}

// @Title   订单会员数据统计
// @Param   sid query   uint64  true "供应商ID"
// @Param	timeType	query	int	false "时间类型[0=不限,1=年,2=月,3=日]"
// @Param	t	query	string	false "日期[yyyy-MM-dd HH:mm:ss]默认:当前时间"
// @Success 200
// @router /orderMemberCount [get]
func (c *OrdersController) OrderMemberCount() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	timeType, _ := c.GetInt("timeType", 0)
	t, _ := utils.StringParseTime(c.GetString("t"))
	c.JSONSuccess(services.OrderServ.OrderMemberCount(uint64(sid), models.OrderTimeType(timeType), t))
}

// @Title   订单地区统计
// @Param   sid query   uint64  true "供应商ID"
// @Param	timeType	query	int	false "时间类型[0=不限,1=年,2=月,3=日]"
// @Param	t	query	string	false "日期[yyyy-MM-dd HH:mm:ss]默认:当前时间"
// @Success 200
// @router /orderLocalCount [get]
func (c *OrdersController) OrderLocalCount() {
	sid, err := c.GetInt64("sid")
	if err != nil {
		c.JSONFailedError(utils.NotFoundSid)
		return
	}
	timeType, _ := c.GetInt("timeType", 0)
	t, _ := utils.StringParseTime(c.GetString("t"))
	c.JSONSuccess(services.OrderServ.OrderLocalCount(uint64(sid), models.OrderTimeType(timeType), t))
}
